package com.czmh.etc.onlinepubsys.controller;

import com.czmh.etc.onlinepubsys.constants.GlobalStatusCode;
import com.czmh.etc.onlinepubsys.exceptions.ModelNotFoundException;
import com.czmh.etc.onlinepubsys.json.Json;
import com.czmh.etc.onlinepubsys.request.DeliverETCEquipmentRequest;
import com.czmh.etc.onlinepubsys.request.ETCApplyApproveRequest;
import com.czmh.etc.onlinepubsys.services.EtcOrderService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Huangyk on 2019/6/24.
 */
@RestController
@RequestMapping("/backSystem")
@Api(tags = "后台系统接口服务")
public class BackSystemController {
    private Logger logger = LoggerFactory.getLogger(BackSystemController.class);

    @Autowired
    private EtcOrderService etcOrderService;

    @PostMapping("/approveETCApply")
    public Json approveETCApply(@RequestBody ETCApplyApproveRequest request){
        Json json = new Json();

        List<String> applyIdList = request.getApplyIdList();
        boolean updateSuccess = false;

        for(String applyId : applyIdList){
            try {
                if(applyId != null && applyId.trim().length() > 0){
                    etcOrderService.approveETCApply(applyId, request.getPassed(), request.getRejectReason(), request.getApproverUserId());

                    updateSuccess = (updateSuccess || true);
                }
            }catch (ModelNotFoundException exp){
                exp.printStackTrace();
                logger.warn("后台系统接口服务，审批申请，没有找到对应的申请实体。applyId：\"" + applyId + "\"");
            }catch (Exception exp){
                exp.printStackTrace();
                logger.warn("后台系统接口服务，审批申请，审批报错！！");
            }
        }

        if(updateSuccess == true){
            json.setErrcode(GlobalStatusCode.SUCCESS);
            json.setErrmsg(GlobalStatusCode.SUCCESS_MSG);
        }else{
            json.setErrcode(GlobalStatusCode.FAILED);
            json.setErrmsg(GlobalStatusCode.FAILED_MSG);
        }

        return json;
    }

    @PostMapping("/deliverETCEquipment")
    public Json deliverETCEquipment(@RequestBody DeliverETCEquipmentRequest request){
        Json json = new Json();
        boolean updateSuccess = false;

        for(DeliverETCEquipmentRequest.DeliverETCDetail detail : request.getDetailList()){
            try {
                etcOrderService.deliverETCEquipment(detail, request.getDeliverUserId(), request.getDeliverTime());

                updateSuccess = (updateSuccess || true);
            }catch (ModelNotFoundException exp){
                exp.printStackTrace();
                logger.warn("后台系统接口服务，ETC设备发货，没有找到对应的订单实体。orderId：\"" + detail.getOrderId() + "\"");
            }catch (Exception exp){
                exp.printStackTrace();
                logger.warn("后台系统接口服务，ETC设备发货，更新信息出错！错误信息：\"" + exp.getMessage() + "\"！");
            }
        }

        if(updateSuccess == true){
            json.setErrcode(GlobalStatusCode.SUCCESS);
            json.setErrmsg(GlobalStatusCode.SUCCESS_MSG);
        }else{
            json.setErrcode(GlobalStatusCode.FAILED);
            json.setErrmsg(GlobalStatusCode.FAILED_MSG);
        }

        return json;
    }
}
